﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YXShop.TemplateAction
{
    public partial class LabelParse
    {
        /// <summary>
         /// 商铺列表
         /// </summary>
         /// <returns></returns>
        public string StoreList()
        {
            string mystyle = this.Label_Inserted;
            string str_CompositorType = this.GetParamValue("YX:CompositorType");//是否排序
            string str_CompositorField = this.GetParamValue("YX:CompositorField");//排序字段
            string str_ShowRows = this.GetParamValue("YX:ShowRows");//显示行数
            string str_ShowColumns = this.GetParamValue("YX:ShowColumns");//显示列数
            string NameCharacter = this.GetParamValue("YX:NameCharacter");//名称长度
            string str_WhetherPagination = this.GetParamValue("YX:WhetherPagination");//是否分页
            string IsAudit = this.GetParamValue("YX:IsAudit");//是否审核
            string str_TempletLayout = this.GetParamValue("YX:TempletLayout");//布局方式
            string WhetherOmit = this.GetParamValue("YX:WhetherOmit");//是否省略
            string ContentCharacter = this.GetParamValue("YX:ContentCharacter");//内容长度
            string comd = string.Empty;//条件
            string order = string.Empty;//排序

            #region 搜索
            string shoptype = ChangeHope.WebPage.PageRequest.GetString("q_shoptype");
            if (shoptype != "")
            {
                comd += " and shoptype=" + shoptype + "";
            }
            string shopKey = ChangeHope.WebPage.PageRequest.GetFormString("q_StoreKey");
            if (shopKey != "")
            {
                comd += " and shopname like '%" + shopKey + "%'";
            }
            string storeHost = ChangeHope.WebPage.PageRequest.GetFormString("q_StoreHost");
            if (storeHost != "")
            {
                comd += " and uid in (select UID from yxs_memberaccount where UserId='" + storeHost + "') ";
            }
            string storeType = ChangeHope.WebPage.PageRequest.GetFormString("q_StoreType");
            if (storeType != "")
            {
                comd += " and shoptype=" + storeType + "";
            }
            #endregion
            if (str_CompositorType == "0")
            {
                if (str_CompositorType == "0")
                {
                    order = " [order by] " + str_CompositorField + " asc";
                }
                else if (str_CompositorType == "1")
                {
                    order = " [order by] " + str_CompositorField + " Desc";
                }
            }
            if (IsAudit == "0")
            {
                comd += " and state=0";
            }
            else if (IsAudit == "1")
            {
                comd += " and state=1";
            }
            
            int rows = 1;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_ShowRows))
            {
                rows =Convert.ToInt32(str_ShowRows);
            }
            int colums = 1;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_ShowColumns))
            {
                colums =Convert.ToInt32(str_ShowColumns);
            }
            int pagesize = rows * colums;

            StringBuilder shtml = new StringBuilder();
            YXShop.BLL.Shop.Shop bll = new YXShop.BLL.Shop.Shop();
            ChangeHope.DataBase.DataByPage dataPage = bll.GetList(order, pagesize, comd);

            YXShop.BLL.Product.Productclass probll = new YXShop.BLL.Product.Productclass();
            YXShop.BLL.Product.ProductInfo pibll = new YXShop.BLL.Product.ProductInfo();
            if (!string.IsNullOrEmpty(mystyle))
            {
                if (dataPage.DataReader != null)
                {
                    YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
                    while (dataPage.DataReader.Read())
                    {
                        string strContent = mystyle;
                        //店铺名称
                        if (strContent.IndexOf("[#StoreName]") > -1)
                        {
                            string shopName = dataPage.DataReader["shopname"].ToString();
                            int CharacterCount=4;
                            if(!string.IsNullOrEmpty(NameCharacter))
                            {
                                if(ChangeHope.Common.ValidateHelper.IsNumber(NameCharacter))
                                {
                                    CharacterCount=Convert.ToInt32(NameCharacter);
                                }
                            }
                            if(WhetherOmit=="True")
                            {
                                shopName=ChangeHope.Common.StringHelper.SubStringAndAppend(shopName,CharacterCount,"...");
                            }
                            else
                            {
                                shopName=ChangeHope.Common.StringHelper.SubStringAndAppend(shopName,CharacterCount,"");
                            }

                            if (shopName != "")
                            {
                                strContent = strContent.Replace("[#StoreName]", shopName);
                            }
                            else
                            {
                                strContent = strContent.Replace("[#StoreName]", "");
                            }
                        }
                        //主营产品
                        if (strContent.IndexOf("[#BusinessProduct]") > -1)
                        {
                            string BusinessProductId = dataPage.DataReader["categories"].ToString();
                            YXShop.Model.Product.Productclass promodel = probll.GetModelID(Convert.ToInt32(BusinessProductId));
                            string BusinessProductName = string.Empty;
                            if (promodel != null)
                            {
                                BusinessProductName = promodel.Name;
                            }
                            strContent = strContent.Replace("[#BusinessProduct]", BusinessProductName);
                        }
                        //店铺图标
                        if (strContent.IndexOf("[#StoreLogo]") > -1)
                        {
                            string shopsign = dataPage.DataReader["shopsign"].ToString();
                            if (shopsign != "")
                            {
                                strContent = strContent.Replace("[#StoreLogo]", sp.DummyPaht+shopsign);
                            }
                            else
                            {
                                strContent = strContent.Replace("[#StoreLogo]", sp.DummyPaht + "images/NaughtImages.gif");
                            }
                        }
                        //店铺描述
                        if (strContent.IndexOf("[#StoreDiscription]") > -1)
                        {
                            string shopIntroduction = dataPage.DataReader["discription"].ToString();
                            int ContentCount=4;
                            if(!string.IsNullOrEmpty(ContentCharacter))
                            {
                                if(ChangeHope.Common.ValidateHelper.IsNumber(ContentCharacter))
                                {
                                    ContentCount=Convert.ToInt32(ContentCharacter);
                                }
                            }
                            if(WhetherOmit=="True")
                            {
                                shopIntroduction=ChangeHope.Common.StringHelper.SubStringAndAppend(shopIntroduction,ContentCount,"...");
                            }
                            else
                            {
                                shopIntroduction=ChangeHope.Common.StringHelper.SubStringAndAppend(shopIntroduction,ContentCount,"");
                            }

                            if (shopIntroduction != "")
                            {
                                strContent = strContent.Replace("[#StoreDiscription]", shopIntroduction);
                            }
                            else
                            {
                                strContent = strContent.Replace("[#StoreDiscription]", shopIntroduction);
                            }
                        }
                        //开店铺时间
                        if (strContent.IndexOf("[#CreateStoreTime]") > -1)
                        {
                            string CreateStoreTime = Convert.ToDateTime(dataPage.DataReader["addtime"].ToString()).ToString("yyyy-MM-dd"); ;
                            if (CreateStoreTime != "")
                            {
                                strContent = strContent.Replace("[#CreateStoreTime]", CreateStoreTime);
                            }
                            else
                            {
                                strContent = strContent.Replace("[#CreateStoreTime]", CreateStoreTime);
                            }
                        }
                        //店铺等级
                        if(strContent.IndexOf("[#StoreLevel]")>-1)
                        {
                            YXShop.BLL.Shop.ShopLevel slbll = new YXShop.BLL.Shop.ShopLevel();
                            string shoprank = dataPage.DataReader["shoprank"].ToString();
                            if (shoprank != "")
                            {
                                YXShop.Model.Shop.ShopLevel slmodel = slbll.GetModelByID(Convert.ToInt32(shoprank));
                                string rankName = string.Empty;
                                if (slmodel != null)
                                {
                                    rankName = slmodel.Name;
                                }
                                strContent = strContent.Replace("[#StoreLevel]", rankName);
                            }
                            else
                            {
                                strContent = strContent.Replace("[#StoreLevel]", "");
                            }
                        }

                        //店铺主人
                        if (strContent.IndexOf("[#StoreHost]") > -1)
                        {
                            YXShop.BLL.Member.MemberAccount mabll = new YXShop.BLL.Member.MemberAccount();
                            string uid = dataPage.DataReader["uid"].ToString();
                            if (uid != "")
                            {
                                YXShop.Model.Member.MemberAccount mamodel = mabll.GetModel(Convert.ToInt32(uid));
                                string userName = string.Empty;
                                if (mamodel != null)
                                {
                                    userName = mamodel.UserId;
                                }
                                strContent = strContent.Replace("[#StoreHost]", userName);
                            }
                            else
                            {
                                strContent = strContent.Replace("[#StoreHost]", "");
                            }
                        }

                        //店铺商品数
                        if (strContent.IndexOf("[#StoreProductCount]") > -1)
                        {
                            string uid = dataPage.DataReader["uid"].ToString();
                            if (uid != "")
                            {
                                System.Data.DataTable dt = pibll.DTGetListWhere(" and pro_PutoutID=" + uid + " and pro_PutoutType=1");
                                int proCount = 0;
                                if (dt.Rows.Count>0)
                                {
                                    proCount = dt.Rows.Count;
                                }
                                strContent = strContent.Replace("[#StoreProductCount]", proCount.ToString());
                            }
                            else
                            {
                                strContent = strContent.Replace("[#StoreProductCount]", "");
                            }
                        }

                        
                        //店铺链接地址
                        if (strContent.IndexOf("[#StoreUrl]") > -1)
                        {
                            //string uid = dataPage.DataReader["uid"].ToString();
                            //if (uid != "")
                            //{
                            string storeUrl = Common.UrlStr("store_index", "StoreId=" + dataPage.DataReader["sid"].ToString() + ";UserId=" + dataPage.DataReader["uid"].ToString() + "", "shop/");
                                strContent = strContent.Replace("[#StoreUrl]", storeUrl);
                            //}
                            //else
                            //{
                            //    strContent = strContent.Replace("[#StoreUrl]", "");
                            //}
                        }

                        //店铺所在所在地区
                        if (strContent.IndexOf("[#StoreArea]") > -1)
                        {
                            YXShop.BLL.Member.MemberInfo mibll = new YXShop.BLL.Member.MemberInfo();
                            string uid = dataPage.DataReader["uid"].ToString();
                            YXShop.Model.Member.MemberInfo mimodel = mibll.GetModel(Convert.ToInt32(uid));
                            if (mimodel != null)
                            {
                                strContent = strContent.Replace("[#StoreArea]",mimodel.Address);
                            }
                        }
                        shtml.Append(strContent);
                        
                    }
                }
                if (str_WhetherPagination == "1")
                {
                    shtml.Append(dataPage.ReceptionToolBar);
                }
            }
            return shtml.ToString();
        }
    }
}
